package com.chocus.core.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chocus.core.bean.product.Brand;
import com.chocus.core.bean.product.Color;
import com.chocus.core.bean.product.Product;
import com.chocus.core.service.product.BrandService;
import com.chocus.core.service.product.ColorService;
import com.chocus.core.service.product.ProductService;

import cn.itcast.common.page.Pagination;

@Controller
@RequestMapping("/product/")
public class ProductController {

	@Autowired
	private ProductService productService;

	@Autowired
	private BrandService brandService;

	@Autowired
	private ColorService colorService;

	/**
	 * 商品列表
	 * 
	 * @param pageNo
	 * @param name
	 * @param isShow
	 * @param brandId
	 * @param model
	 * @return
	 */
	@RequestMapping("list.do")
	public String list(Integer pageNo, String name, Boolean isShow, Long brandId, Model model) {
		// 查询品牌列表
		List<Brand> brandList = brandService.getBrandListByQuery();
		model.addAttribute("brandList", brandList);
		// 查询商品列表
		Pagination pagination = productService.getProductList(pageNo, name, isShow, brandId);
		model.addAttribute("pagination", pagination);
		model.addAttribute("name", name);
		if (isShow != null) {
			model.addAttribute("isShow", isShow);
		} else {
			model.addAttribute("isShow", false);
		}
		model.addAttribute("brandId", brandId);
		return "product/list";
	}

	/**
	 * 前往添加页面
	 * 
	 * @param pageNo
	 * @param name
	 * @param isShow
	 * @param brandId
	 * @param model
	 * @return
	 */
	@RequestMapping("toAdd.do")
	public String toAdd(Model model) {
		// 查询品牌列表
		List<Brand> brandList = brandService.getBrandListByQuery();
		model.addAttribute("brandList", brandList);
		List<Color> colorList = colorService.getColorList();
		model.addAttribute("colorList", colorList);
		return "product/add";
	}

	/**
	 * 添加
	 * 
	 * @return
	 */
	@RequestMapping("add.do")
	public String add(Product product) {
		productService.insertProduct(product);
		return "redirect:list.do";
	}

	/**
	 * 上架
	 * 
	 * @return
	 */
	@RequestMapping("isShow.do")
	public String add(Long ids[]) {
		productService.isShow(ids);
		return "redirect:list.do";
	}
}
